<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\admin\model;

use think\Model;

class class_blogModel extends Model
{

	protected $type = [
        'more' => 'array',
    ];

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    /**
     * 关联 user表
     * @return $this
     */
    public function user()
    {
        return $this->belongsTo('UserModel', 'user_id');
    }

    /**
     * post_content 自动转化
     * @param $value
     * @return string
     */
    public function getPostContentAttr($value)
    {
        return cmf_replace_content_file_url(htmlspecialchars_decode($value));
    }

    /**
     * post_content 自动转化
     * @param $value
     * @return string
     */
    public function setPostContentAttr($value)
    {
        return htmlspecialchars(cmf_replace_content_file_url(htmlspecialchars_decode($value), true));
    }

    /**
     * published_time 自动完成
     * @param $value
     * @return false|int
     */
    public function setTimeAttr($value)
    {
		if($value<=0){
			$value = 0;
		}
        return strtotime($value);
    }

	public function find(){
		$data = parent::find();
		$data['content'] = $this->setPostContentAttr($data['content']);
		return $data;
	}
	
    /**
     * 后台管理添加文章
     * @param array $data 文章数据
     * @return $this
     */
    public function adminAddArticle($data)
    {
        $data['user_id'] = cmf_get_current_admin_id();
		$data['content'] = empty($data['content'])?'':$this->getPostContentAttr($data['content']);
		
        $data['status'] = '1';
		$data['post_status'] = empty($data['post_status']) ? 0 : 1;
		$data['post_status_delete'] = empty($data['post_status_delete']) ? 0 : 1;
        $data['is_top']      = empty($data['is_top']) ? 0 : 1;
		
		
		if($data['post_status']){
			$data['publish_time'] = $this->setTimeAttr($data['publish_time']);
		}
		else{
			$data['publish_time'] = 0;
		}
		if($data['post_status_delete']){
			$data['delete_time'] = $this->setTimeAttr($data['delete_time']);
		}
		else{
			$data['delete_time'] = '2493046800';
		}

        $this->allowField(true)->data($data, true)->isUpdate(false)->save();

        return $this;

    }

    /**
     * 后台管理编辑文章
     * @param array $data 文章数据
     * @param array|string $categories 文章分类 id
     * @return $this
     */
    public function adminEditArticle($data,$blog_id)
    {

		$data['user_id'] = cmf_get_current_admin_id();
		$data['content'] = empty($data['content'])?'':$this->getPostContentAttr($data['content']);
		
        $data['status'] = '1';
		$data['post_status'] = empty($data['post_status']) ? 0 : 1;
		$data['post_status_delete'] = empty($data['post_status_delete']) ? 0 : 1;
        $data['is_top']      = empty($data['is_top']) ? 0 : 1;
		

		if($data['post_status']){
			$data['publish_time'] = $this->setTimeAttr($data['publish_time']);
		}
		else{
			$data['publish_time'] = 0;
		}
		if($data['post_status_delete']){
			$data['delete_time'] = $this->setTimeAttr($data['delete_time']);
		}
		else{
			$data['delete_time'] = '2493046800';
		}

		
        $this->allowField(true)->isUpdate(true)->data($data, true)->save(['id'=>$blog_id]);

        return $this;

    }
	
	public function select(){
		$data = parent::select();
		return $data;
	}

    public function adminDeletePage($data)
    {

        if (isset($data['id'])) {
            $id = $data['id']; //获取删除id

            $res = $this->where(['id' => $id])->find();

            if ($res) {
                $res = json_decode(json_encode($res), true); //转换为数组

                $recycleData = [
                    'object_id'   => $res['id'],
                    'create_time' => time(),
                    'table_name'  => 'portal_post#page',
                    'name'        => $res['post_title'],

                ];

                Db::startTrans(); //开启事务
                $transStatus = false;
                try {
                    Db::name('portal_post')->where(['id' => $id])->update([
                        'delete_time' => time()
                    ]);
                    Db::name('recycle_bin')->insert($recycleData);

                    $transStatus = true;
                    // 提交事务
                    Db::commit();
                } catch (\Exception $e) {

                    // 回滚事务
                    Db::rollback();
                }
                return $transStatus;


            } else {
                return false;
            }
        } elseif (isset($data['ids'])) {
            $ids = $data['ids'];

            $res = $this->where(['id' => ['in', $ids]])
                ->select();

            if ($res) {
                $res = json_decode(json_encode($res), true);
                foreach ($res as $key => $value) {
                    $recycleData[$key]['object_id']   = $value['id'];
                    $recycleData[$key]['create_time'] = time();
                    $recycleData[$key]['table_name']  = 'portal_post';
                    $recycleData[$key]['name']        = $value['post_title'];

                }

                Db::startTrans(); //开启事务
                $transStatus = false;
                try {
                    Db::name('portal_post')->where(['id' => ['in', $ids]])
                        ->update([
                            'delete_time' => time()
                        ]);


                    Db::name('recycle_bin')->insertAll($recycleData);

                    $transStatus = true;
                    // 提交事务
                    Db::commit();

                } catch (\Exception $e) {

                    // 回滚事务
                    Db::rollback();


                }
                return $transStatus;


            } else {
                return false;
            }

        } else {
            return false;
        }
    }

}